/*
 *  session.cpp
 */

#include <cstdlib>
#include <cstring>
#include <string>
#include "debug.h"
#include "message.h"
#include "message_dispatcher.h"
#include "mutex.h"
#include "protocol.h"
#include "thread.h"
#include "session.h"
#include "socket.h"

Session::Session(std::string sessionID, Socket* sock, MessageQueue& q)
                :proto(),
                 receiver(this, q),
                 dispatcher(this),
                 t_receiver(receiver),
                 t_dispatcher(dispatcher) {
                
    this->sessionID = sessionID;
    this->sock = sock;
    alive = true;
    t_receiver.start();
    t_dispatcher.start();
}

Session::~Session() {
    alive = false;
    delete sock;
    t_receiver.join();
    t_dispatcher.join();
}

std::string Session::getID() {
    return sessionID;
}

bool Session::isAlive() {
    return alive;
}

void Session::kill() {
    alive = false;
}

void Session::sendMessage(Message* msg) {
    dispatcher.send(msg);
}

//should change to friend
Protocol& Session::getProtocol() {
    return proto;
}

//should change to friend
Socket* Session::getSocket() {
    return sock;
}

//{+}

